package com.yjhb.android.sbz.mvp.common;

import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.entity.UserInfo;
import com.yjhb.android.sbz.http.HttpObserver;
import com.yjhb.android.sbz.http.HttpTool;
import com.yjhb.android.sbz.manager.SysManager;
import com.yjhb.android.sbz.manager.im.IMManager;
import com.yjhb.android.sbz.mvp.ConventionPresenter;
import com.jojo.android.zxlib.base.mvp.CommonView;
import com.jojo.android.zxlib.tool.ZxLogTool;
import com.jojo.android.zxlib.tool.ZxNetTool;
import com.jojo.android.zxlib.tool.ZxToast;

import org.greenrobot.eventbus.EventBus;

/**
 * 创建者 zx
 * 创建时间 2020/11/24 11:16
 */
public class CommonPresenter extends ConventionPresenter<CommonView> {
    private final String TAG = "CommonPresenter";

    public CommonPresenter(CommonView baseView) {
        super(baseView);
    }

    @Override
    protected void onDestroy() {

    }


    public void updateUserInfo() {
        if (checkUnLogin()) return;
        if (model!=null){
            HttpTool.request(model.getUserInfo(App.userInfo.getId()), new HttpObserver<UserInfo>() {
                @Override
                public void onResult(UserInfo data) {
                    App.updateUserInfo(data);
                    EventBus.getDefault().post(new EventMsg(EventMsg.USER_UPDATE_OK, data));
                    IMManager.getInstance().login();
                    if (data != null)
                        ZxLogTool.setUserId(data.getId());
                }

                @Override
                public void onError(int code, String msg) {
                    ZxLogTool.e("UserInfo Err" + msg);
                    if (code == 403 || code == 604) {
                        SysManager.clearUserInfo();
                        return;
                    }
                    boolean b = ZxNetTool.isNetworkAvailable(App.getInstance());
                    if (b) {
                        ZxToast.showCenterToastLight("用户信息获取失败，服务器开小差了");
                    } else {
                        ZxToast.showCenterToastLight("用户信息获取失败，请检查您的网络");
                    }
                }
            });

        }
    }


}
